package character.ai;

import character.CharacterStatusType;
import character.MyCharacter;
import java.util.TreeMap;

public class ThreatSystem {
    protected AIGeneralControl aiGeneralControl;
    protected TreeMap<CharacterThreat, CharacterThreat> threatTable = new TreeMap<CharacterThreat, CharacterThreat>(new CharacterThreatComparator());
    
    @Deprecated
    protected ThreatSystem() {}
    
    public ThreatSystem(AIGeneralControl aiGeneralControl) {
        this.aiGeneralControl = aiGeneralControl;
    }
    
    public MyCharacter getHighestThreatCharacter() {
        switch(aiGeneralControl.getAILogicSettings().getThreatEvaluation()) {
            case CLOSEST:
                return aiGeneralControl.getClosestHostileCharacter();
            case MOST_DAMAGE:
                if(!threatTable.isEmpty()) {
                    for(CharacterThreat characterThreat : threatTable.keySet()) {
                        if(characterThreat.getCharacter().getCharacterStatus().getCharacterStatusType() != CharacterStatusType.DEAD)
                            return characterThreat.getCharacter();
                    }
                }
                return aiGeneralControl.getClosestHostileCharacter();
            default:
                return null;
        }
    }
    
    public void generateThreat(MyCharacter fromCharacter, int damageTaken) {
        CharacterThreat characterThreat = new CharacterThreat(fromCharacter);
        if(!threatTable.containsKey(characterThreat)) {
            threatTable.put(characterThreat, characterThreat);
        }
        threatTable.get(characterThreat).addThreat(damageTaken);
    }
}
